//
//  PopUtil.m
//  RelianceSmartFire
//
//  Created by 王昭 on 2017/7/1.
//  Copyright © 2017年 reliance. All rights reserved.
//

#import "PopUtil.h"

static CGFloat alertWidth = 200.0f;
static CGFloat alertHeight = 50.0f;
static CGFloat alertBottom = 100.0f;
static CGFloat titleMargin = 20.0f;
static CGFloat naviHeight = 64.0f;

@implementation PopUtil

+(void)showMessage:(NSString *)message{
    [PopUtil showMessage:message positionType:PopPositionTypeMiddle];
}

+(void)showMessage:(NSString *)message positionType:(PopPositionType)type
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    
    UIView *alert = [[UIView alloc] init];
    alert.backgroundColor = [UIColor blackColor];
    alert.alpha = 0.8f;
    alert.layer.cornerRadius = 5.0f;
    alert.layer.masksToBounds = YES;
    [window addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(alertWidth));
        make.height.equalTo(@(alertHeight));
        make.centerX.equalTo(window.mas_centerX);
        if (type == PopPositionTypeBottom) {
            make.bottom.equalTo(window.mas_bottom).offset(-alertBottom);
        }
        else{
            make.centerY.equalTo(window.mas_centerY);
        }
    }];
    
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize titleSize = [message boundingRectWithSize:CGSizeMake(alertWidth-2*titleMargin, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
    UILabel *title = [[UILabel alloc] init];
    title.font = font;
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    title.numberOfLines = 0;
    title.text = message;
    [alert addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(alert.mas_left).offset(titleMargin);
        make.right.equalTo(alert.mas_right).offset(-titleMargin);
        make.top.equalTo(alert.mas_top).offset((alertHeight-titleSize.height)/2.0);
    }];
    title.height = titleSize.height;
    
//    alert.height = titleMargin*2+titleSize.height;
    [UIView animateWithDuration:3.0f animations:^{
        alert.alpha = 0;
    } completion:^(BOOL finished) {
        [alert removeFromSuperview];
    }];
}

@end
